package com.project.schoolFoodShare.controller.type;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.schoolFoodShare.common.enums.BusinessType;
import com.project.schoolFoodShare.common.enums.ResultCode;
import com.project.schoolFoodShare.domain.FoodList;
import com.project.schoolFoodShare.domain.FoodType;
import com.project.schoolFoodShare.domain.Result;
import com.project.schoolFoodShare.service.FoodListService;
import com.project.schoolFoodShare.service.FoodTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @description: 美食类型controller
 */
@Controller
@ResponseBody
@RequestMapping("type")
public class FoodTypeController {

    @Autowired
    private FoodTypeService foodTypeService;
    @Autowired
    private FoodListService foodListService;

    /** 分页获取美食类型 */
    @PostMapping("getFoodTypePage")
    public Result getFoodTypePage(@RequestBody FoodType foodType) {
        Page<FoodType> page = new Page<>(foodType.getPageNumber(),foodType.getPageSize());
        QueryWrapper<FoodType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(foodType.getName()),FoodType::getName,foodType.getName());
        Page<FoodType> foodTypePage = foodTypeService.page(page, queryWrapper);
        return Result.success(foodTypePage);
    }

    @GetMapping("getFoodTypeList")
    public Result getFoodTypeList() {
        List<FoodType> typeList = foodTypeService.list();
        return Result.success(typeList);
    }

    /** 根据id获取美食类型 */
    @GetMapping("getFoodTypeById")
    public Result getFoodTypeById(@RequestParam("id")String id) {
        FoodType foodType = foodTypeService.getById(id);
        return Result.success(foodType);
    }

    /** 保存美食类型 */
    @PostMapping("saveFoodType")
    public Result saveFoodType(@RequestBody FoodType foodType) {
        boolean save = foodTypeService.save(foodType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑美食类型 */
    @PostMapping("editFoodType")
    public Result editFoodType(@RequestBody FoodType foodType) {
        boolean save = foodTypeService.updateById(foodType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除美食类型 */
    @GetMapping("removeFoodType")
    public Result removeFoodType(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                QueryWrapper<FoodList> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(FoodList::getTypeId,id);
                int count = foodListService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("当前美食类型存在美食分享,无法删除");
                }
                foodTypeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("美食类型id不能为空！");
        }
    }

}
